<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

/**
 * 测试上传类
 * Class Upload
 */
class Upload extends CI_Controller {
	public function __construct()
	{
		parent::__construct ();
		$this->load->language('base');
		$this->load->service('login_service');
		$this->load->service("user_files_service");
		if(!$this->login_service->is_login())
		{
			Common::ajax_return(1011);
		}
		else
		{
			$this->load_variable();
		}
	}
	private function load_variable()
	{
		$user_info = $this->login_service->get_user_info();
		$this->uid = $user_info['id'];
		$this->uemail = $user_info['email'];
		$this->sid = $this->session->session_id;
	}
	public function index()
	{

		//sleep(30);
		$pid = $this->input->get_post("folder_id", TRUE);
		$folder_id = 0;
		if(!empty($pid))
		{
			$folder_id = $pid;
		}
		if (!empty($_FILES))
		{
			$temp_file = $_FILES['file']['tmp_name'];
			$mime_type = Common::get_mime_type($temp_file);
			$name = $_FILES ['file'] ['name'];
			$size = $_FILES['file']['size'];
			$md5 = md5_file($temp_file);
			$guid = Common::uniqid();
			$source_guid = Common::uniqid();
			//$add_file = $this->user_files_service->add_file($this->uid,$name,$size,$mime_type,$md5,$guid,$folder_id);
			if(!$this->user_files_service->can_add_file($this->uid,$name,$size,$mime_type))
			{
				Common::ajax_return($this->user_files_service);
			}
			$params = array(
				"uid"=>$this->uid,
				"name"=>$name,
				"size"=>$size,
				"mime_type"=>$mime_type,
				"md5"=>$md5,
				"guid"=>$guid,
				"source_guid"=>$source_guid,
				"folder_id"=>$folder_id,
				"file_path"=>$temp_file
			);
			$default_path = $this->config->item("default_upload_path")."temp/";
			if(!is_dir($default_path))
			{
				@mkdir($default_path,0777,true);
			}
			$save_path = $default_path.$params['source_guid'].".tmp";
			move_uploaded_file($params['file_path'],$save_path);
			$params['file_path']=$save_path;
			$this->add_packet($guid,$params);
			Common::ajax_return(0,NULL,array("guid"=>$guid),FALSE);
			/*if($add_file===false)
			{
				return Common::ajax_return($this->user_files_service);
			}
			else
			{
				$default_path = $this->config->item("default_upload_path");
				if(!is_dir($default_path))
				{
					@mkdir($default_path,0777,true);
				}
				$save_path = $default_path.$add_file['source_guid'];
				move_uploaded_file($temp_file,$save_path);
				//Common::prf_json(true,$add_file);
				$this->add_packet($guid,$params);
				return Common::ajax_return(0,NULL,array("guid"=>$add_file['guid']),FALSE);
			}*/

		} else {
            Common::ajax_return(1003);
        }
	}


	private function add_packet($guid,$params)
	{
		$list = $this->session->userdata("PACKET_LIST");
		/*$sess_data = $this->session->userdata("PACKET_PERCENT");
		if(empty($sess_data) || !isset($sess_data[$guid]))
		{
			$percent = 0;
		}
		else
		{
			$percent = $sess_data[$guid];
		}*/
		if(empty($list))
		{
			$list = array();
		}
		$list[$guid]=array(
			"params"=>$params,
			"data"=>array(),
			"percent"=>0
		);
		$this->session->set_userdata("PACKET_LIST",$list);
	}
	public function qp() {
// 		print_r($_SERVER);
		var_dump($_SERVER['HTTP_X_REQUESTED_WITH']);
		var_dump($_SERVER['X-Progress-ID']);
		die();
		$file_index = $this->input->get ( "_rsvd_index" );
		$paramArr = array(
				"_rsvd_index" => $file_index,
		);
		$pri = array (
				"state" => "uploading",
				"received" => 123,
				"size" => 1234 
		) ;
		common::prf_json(TRUE,$pri,$paramArr);
	}
	
	/**
	 * 添加策略（新建文件夹的时候，继续父文件的策略）
	 * @param string parent_uuid 父文件夹uuid
	 * @param string uuid  文件夹uuid
	 * @param string hash_uuid  文件夹hash_uuid
	 */
	private function add_policies($parent_uuid,$uuid,$hash_uuid){
		//查询文件夹的策略
		$file_policies = $this->db->get_where("file_policy",array(
				"file_uuid" => $parent_uuid
		))->result_array();
	
		if(! $file_policies){
			return true;
		}
	
		$newArr = array();
		foreach ($file_policies as $key => $file_policy)
		{
			$newArr[$key]["file_uuid"] = $uuid;
			$newArr[$key]["hash_file_uuid"] = $hash_uuid;
			$newArr[$key]["pid"] = $file_policy['pid'];
			$newArr[$key]["value_set"] = $file_policy['value_set'];
			$newArr[$key]["value"] = $file_policy['value'];
			$newArr[$key]["creation_ts"] = time()*1000;
		}
	
		$this->load->model('policies_mdl');
		$flag = $this->policies_mdl->addBatch($newArr);
	
		return $flag;
	}
	
	public function d(){
		$filename = 'F:\y.txt';
		//文件的类型
		header('Content-type: text/plain');
		//下载显示的名字
		header('Content-Disposition: attachment; filename="tt.txt"');
// 		readfile("$filename");
		common::prf_json(TRUE,null,null);
	}
}
